﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using YourPersonalTrainer.BusinessLayer;
using MRUSample;

namespace YourPersonalTrainer.ViewLayer
{
    public partial class YourPersonalTrainerUC : Form, IMRUClient
    {
        private Workout m_Workout;
        private Thread m_WorkoutThread;
        private WorkoutProgressUC m_WorkoutProgressWindow;
        private int m_RowRightClickedOn;

        public YourPersonalTrainerUC()
        {
            InitializeComponent();

            m_Workout = new Workout(new WorkoutTimer());
            m_Workout.Add(new WorkoutItem("Tree Pose", 10));
            m_Workout.Add(new WorkoutItem("Standing Forward Bend", 10));
            m_Workout.Add(new WorkoutItem("Tree Pose 2", 5));
            m_Workout.Add(new WorkoutItem("Standing Forward Bend 2", 10));

            //m_MRUManager = new MRUManager();
            //m_MRUManager.Initialize(this, recentFilesMenuItem, @"Software\ClearerCode\YourPersonalTrainer");

            //dataGridView1.DataSource = m_workout;
            workoutItemBindingSource.DataSource = m_Workout;
            UpdateTotal();
        }

        private void workoutItemBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void ExecuteWorkoutButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            m_WorkoutThread = new Thread(m_Workout.Start);
            m_WorkoutProgressWindow = new WorkoutProgressUC();
            m_WorkoutProgressWindow.Initialise(m_Workout);
            m_WorkoutProgressWindow.Show();
            m_WorkoutThread.Start();
        }

        private void UpdateGridView()
        {
            workoutItemBindingSource.DataSource = m_Workout;
            this.BindingContext[dataGridView1.DataSource].EndCurrentEdit();
            dataGridView1.Refresh();
            dataGridView1.Parent.Refresh();
            UpdateTotal();
        }

        private void UpdateTotal()
        {
            totalLabel.Text = "Total Time: " + m_Workout.Duration().ToString();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            DialogResult dialogResult = openFileDialog1.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(m_Workout.GetType());
                TextReader r = new StreamReader(openFileDialog1.FileName);

                m_Workout = (Workout)x.Deserialize(r);
                m_Workout.Timer = new WorkoutTimer();

                r.Close();
                UpdateGridView();
            }


        }

        private void workoutItemBindingSource_DataError(object sender, BindingManagerDataErrorEventArgs e)
        {

        }

        private void RemoveRowMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                workoutItemBindingSource.RemoveAt(dataGridView1.SelectedRows[0].Index);
                UpdateGridView();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = saveFileDialog1.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                m_Workout.SaveToFile(saveFileDialog1.FileName);
            }

        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            UpdateTotal();
        }


        private void YourPersonalTrainerUC_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_Workout.Stop();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            m_Workout.Stop();
        }

        #region IMRUClient Members

        public void OpenMRUFile(string fileName)
        {
            throw new NotImplementedException();
        }

        #endregion

        private void insertMenuItem1_Click(object sender, EventArgs e)
        {
            workoutItemBindingSource.Insert(m_RowRightClickedOn, new WorkoutItem());
            dataGridView1.ClearSelection();
            dataGridView1.Rows[m_RowRightClickedOn].Selected = true;
            dataGridView1.Columns[1].Selected = true;
            UpdateGridView();
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                m_RowRightClickedOn = e.RowIndex;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DialogResult dialogResult = openAudioFileDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                m_Workout[e.RowIndex].AudioFileLocation = openAudioFileDialog.FileName;
                UpdateGridView();
            }
        }
    }
}
